package com.rzico.sbl.ui.report.manager

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.google.android.material.tabs.TabLayoutMediator
import com.rzico.sbl.R
import com.rzico.sbl.databinding.FragmentReportManagerBinding
import com.rzico.sbl.model.ManagerContent
import com.rzico.sbl.other.userRole
import com.rzico.sbl.ui.report.depot.ReportDepotActivity
import com.rzico.sbl.viewmodel.ReportManagerViewModel
import com.xinnuo.common.extend.get
import com.xinnuo.common.extend.startActivityEx
import com.xinnuo.common.helper.getString
import com.xinnuo.common.view.FullyGridLayoutManager
import com.xinnuo.common.decoration.GridVerticalDecoration
import com.xinnuo.common_ui.base.NavigationFragment
import com.xinnuo.common_ui.base.addItems
import com.xinnuo.common_ui.view.BadgeTextView
import com.xinnuo.slimadapter.SlimAdapter

class ReportManagerFragment : NavigationFragment() {

    private var mBinding: FragmentReportManagerBinding? = null

    override fun getViewModel() = get<ReportManagerViewModel>()

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ) = FragmentReportManagerBinding
        .inflate(inflater, container, false)
        .also { mBinding = it }
        .root

    override fun initTitle() {
        initLayout()
        initTabLayout()

        mList.clear()

        when (userRole) {
            "6" -> mList.addItems(getViewModel().generateList1())
            "7" -> mList.addItems(getViewModel().generateList2())
        }

        mAdapter.setDataList(mList)
    }

    /**
     * 工作台
     */
    private fun initLayout() {
        mBinding?.let { binding ->
            binding.managerList.apply {
                isNestedScrollingEnabled = false
                layoutManager = FullyGridLayoutManager(getIContext(), 5)

                addItemDecoration(GridVerticalDecoration(vertical = 15f))

                mAdapter = SlimAdapter.creator()
                    .register<ManagerContent>(R.layout.item_manager_grid) { jector, bean, _ ->

                        // 解构声明
                        val (title, imageId, _, _) = bean

                        jector.apply {
                            text(R.id.manager_hint, title)

                            with<ImageView>(R.id.manager_image) {
                                if (imageId > 0) it.setImageResource(imageId)
                            }

                            with<BadgeTextView>(R.id.manager_num) {
                                it.setBadgeCount(bean.count)
                            }

                            clicked {
                                when (bean.title) {
                                    "经理" -> startActivityEx<ReportManagerActivity>()
                                    "库管" -> startActivityEx<ReportDepotActivity>()
                                    "司机" -> startActivityEx<ReportDriverActivity>()
                                    "空桶" -> startActivityEx<ReportBarrelActivity>()
                                    "水站" -> startActivityEx<ReportStationActivity>()
                                    "实时位置" -> startActivityEx<ReportManagerMapActivity>()
                                }
                            }
                        }
                    }
                    .attachTo(this)
            }

            binding.managerTitle.text = getString("shopName")
        }
    }

    private fun initTabLayout() {
        mBinding?.let {
            // 去除边缘阴影
            it.managerVp
                .getChildAt(0)
                .overScrollMode = View.OVER_SCROLL_NEVER
            // 设置viewpager是否可以左右滑动，默认true
            it.managerVp
                .isUserInputEnabled = true

            // ViewPager2左右滑动时，默认不会预加载，Fragment生命周期
            // 未创建：onCreate → onStart → onPause(上一个Fragment) → onResume
            // 已创建：onPause(上一个Fragment) → onResume
            // 当点击tab时，会执行预加载，Fragment生命周期
            // 未创建时：预加载前一个(onCreate → onStart)
            //          → 当前Fragment(onCreate → onStart)
            //          → 预加载后一个(onCreate → onStart)
            //          → onPause(上一个Fragment) → onResume
            // 已创建：onPause(上一个Fragment) → onResume
            it.managerVp.adapter =
                object : FragmentStateAdapter(this) {
                    override fun getItemCount() = 3
                    override fun createFragment(position: Int) =
                        when (position) {
                            1 -> ManagerSecondFragment()
                            2 -> ManagerThirdFragment()
                            else -> ManagerFirstFragment()
                        }
                }

            TabLayoutMediator(
                it.managerTab,
                it.managerVp
            ) { tab, positon ->
                tab.text = when (positon) {
                    0 -> "收入"
                    1 -> "订单"
                    2 -> "商品"
                    else -> ""
                }
            }.attach()
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        mBinding = null
    }

}